﻿using CSCommonUtils;
using CsharpHttpHelper;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FX_API.Request
{
    public abstract class FXBasicRequest
    {
        protected readonly HttpHelper helper = new HttpHelper();
        public abstract string url { get; set; }

        public delegate void ExceptionHandler(Exception ex);
        public ExceptionHandler exceptionHandler;

        public ResultMessage SendRequest(object obj)
        {
            HttpItem httpItem = new HttpItem()
            {
                URL = url,
                ContentType = "application/json",
                Accept = "application/json",
                Method = "POST",
                //Encoding = Encoding.UTF8,
                PostEncoding = Encoding.UTF8,
                Postdata = JsonConvert.SerializeObject(obj)
            };

            var result = helper.GetHtml(httpItem);

            if (result.StatusCode != System.Net.HttpStatusCode.OK) return new ResultMessage() { IsSucess = false, Message = result.Html };
            return new ResultMessage() { IsSucess = true, Message = result.Html };
        }
    }
}
